package com.kuzufang.modules.main.roommate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.kuzufang.R;
import com.kuzufang.base.BaseAdapter;
import com.kuzufang.bean.RoommateBean;
import com.kuzufang.modules.imageutil.ImageHandler;
import com.kuzufang.modules.imageutil.ImageUrls;
import com.kuzufang.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyuanjin on 2017/9/1.
 */

public class RoommateAdapter extends BaseAdapter<RoommateBean, RoommateAdapter.RoommateViewHolder> {
    private Activity activity;

    public RoommateAdapter(Context ctx) {
        super(ctx);
        activity = (Activity) ctx;
    }

    public void setData() {
        clear();
        List<RoommateBean> roommateBeen = new ArrayList<>();
        List<String> images = ImageUrls.imageUrls();
        for (int i = 0; i < images.size(); i++) {
            RoommateBean roommateBean = new RoommateBean();
            roommateBean.setUrl(images.get(i));
            roommateBeen.add(roommateBean);
        }
        addAll(roommateBeen);
        notifyItemRangeInserted(0, roommateBeen.size() - 1);
    }

    @Override
    protected void bindViewHolderData(RoommateViewHolder viewHolder, RoommateBean data, int position) {
        viewHolder.bindData(data, position);
    }

    @Override
    public RoommateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.item_roommate_main, parent, false);
        return new RoommateViewHolder(view);
    }

    class RoommateViewHolder extends RecyclerView.ViewHolder {
        private ImageView roomImageView;
        private CardView itemCard;

        public RoommateViewHolder(View itemView) {
            super(itemView);
            itemCard = (CardView) itemView.findViewById(R.id.item_card);
            roomImageView = (ImageView) itemView.findViewById(R.id.iv_room);
        }

        public void bindData(final RoommateBean bean, int position) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemCard.getLayoutParams();
            if (position == 0) {
                layoutParams.topMargin = 0;
            } else {
                layoutParams.topMargin = DisplayUtils.dp2px(12);
            }
            itemCard.setLayoutParams(layoutParams);

            ImageHandler.getImageLoader().load(itemView.getContext(), bean.getUrl(), roomImageView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, RoommateDetailActivity.class);
                    intent.putExtra("room_detail", bean.getUrl());
                    String transitionName = itemView.getContext().getString(R.string.transition_album_cover);
                    ActivityOptionsCompat options =
                            ActivityOptionsCompat.makeScaleUpAnimation(roomImageView,
                                    roomImageView.getWidth() / 2, roomImageView.getHeight() / 2, 0, 0);
                    ActivityCompat.startActivity(itemView.getContext(), intent, options.toBundle());
                }
            });
        }
    }
}
